/*
 * LineBorder.java
 */

package xq.swing.borders;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.border.Border;

/**
 *
 * @author ThorntonRP
 */
public class LineBorder implements Border {

	private static final long serialVersionUID = 706585605778243786L;
	private Color color;
	private Insets borderInsets;
	private Insets marginInsets;
	private Insets combinedInsets;

	public LineBorder(Color color, Insets borderInsets, Insets marginInsets) {
		this.color = color;
		this.borderInsets = borderInsets;
		this.marginInsets = marginInsets;
		this.combinedInsets = new Insets(
				borderInsets.top + marginInsets.top,
				borderInsets.left + marginInsets.left,
				borderInsets.bottom + marginInsets.bottom,
				borderInsets.right + marginInsets.right);
	}

	@Override
	public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
		Color oldColor = g.getColor();
		g.setColor(color);
		if (borderInsets.top > 0) {
			g.fillRect(x, y, width, borderInsets.top);
		}
		if (borderInsets.left > 0) {
			g.fillRect(x, y, borderInsets.left, height);
		}
		if (borderInsets.bottom > 0) {
			g.fillRect(x, y + height - borderInsets.bottom, width, borderInsets.bottom);
		}
		if (borderInsets.right > 0) {
			g.fillRect(x + width - borderInsets.right, y, borderInsets.right, height);
		}
		g.setColor(oldColor);
	}

	@Override
	public Insets getBorderInsets(Component c) {
		return (Insets) combinedInsets.clone();
	}

	@Override
	public boolean isBorderOpaque() {
		return false;
	}
}
